<?php
/*
 Template Name: Openid Template
*/
?>
<?php
require_once 'openid.php';

const DOMAIN_NAME ="localhost";
$urlGoogle ="";
$urlYahoo ="";
$openidGoogle = new LightOpenID(DOMAIN_NAME);
$openidYahoo = new LightOpenID(DOMAIN_NAME);


add_action('wp_head', 'openIdGoogleLogin');
add_action('wp_head', 'openIdYahooLogin');

function openIdGoogleLogin(){
		if(!$openidGoogle->mode) {
			$openidGoogle->identity = 'https://www.google.com/accounts/o8/id';
			$openidGoogle->required = array('contact/email', 'pref/language', 'namePerson/friendly','namePerson', 'birthDate', 'person/gender');
			$urlGoogle = $openidGoogle->authUrl();
		} elseif($openidGoogle->mode == 'cancel') {
			//echo 'User has canceled authentication!';
			$urlGoogle = "canceled";
		} else {
			$urlGoogle = "logined";
			$Googledata = $openidGoogle->getAttributes();
			
			$username = "";
			$pass ="";
			//check user is exit or not
			$myrows = $wpdb->get_results( "SELECT *
										FROM ".$wpdb->users."
										WHERE user_email='".$Googledata['contact/email']."'");

			if(sizeof($myrows) == 0){
				$dataEmail = explode("@", $Googledata['contact/email']);
				$username = $dataEmail[0];
				$pass = "yourstyle!@#".rand();
				
				//insert user to database.
				$uid = wp_insert_user( array ('user_login' => $dataEmail[0], 
											'display_name' => $Googledata['namePerson'], 
											'user_email' => $Googledata['contact/email'],
											'user_pass' => $pass)) ;
			}
			else{
				$uid =$myrows[0]->ID;
				$username = $myrows[0]->user_login;
				$pass = $myrows[0]->user_pass;
			}
			
			$creds = array('user_login' => $username,
					'user_password' =>$pass,
					'remember' => true
					);
			
			// TODO do we deprecate the wp_authentication action?
			do_action_ref_array('wp_authenticate', array(&$creds['user_login'], &$creds['user_password']));
			$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, $credentials);
			
			$user = wp_authenticate($creds['user_login'], $creds['user_password']);
			wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie);
		}
		echo "<input type='hidden' id='igoogleid' value='".$urlGoogle."' />";
		
		
}
function openIdYahooLogin(){
	echo "HUHUHUHU"; exit;
	if(!$openidYahoo->mode) {
		$openidYahoo->identity = 'https://me.yahoo.com';
		$openidYahoo->required = array('contact/email', 'pref/language', 'namePerson/friendly','namePerson', 'birthDate', 'person/gender');
		$urlYahoo = $openidYahoo->authUrl();
	} elseif($openidYahoo->mode == 'cancel') {
		$urlYahoo = "canceled";
	} else {
		$urlYahoo = "logined";
		$Yahoodata = $openidYahoo->getAttributes();
	
		//check user is exit or not
		$myrows = $wpdb->get_results( "SELECT *
											FROM ".$wpdb->users."
											WHERE user_email='".$Yahoodata['contact/email']."'");
		if( sizeof($myrows) == 0){
			$dataEmail = explode("@", $Yahoodata['contact/email']);
			$password = "12345";
			//insert user to database.
			$uid = wp_insert_user( array ('user_login' => $dataEmail[0],
					'display_name' => $Yahoodata['namePerson'],
					'user_email' => $Yahoodata['contact/email'],
					'user_pass' => $password)) ;
		}
		else{
			$uid =$myrows[0]->ID;
		}

		$creds = array();
		$creds['user_login'] = "admin";
		$creds['user_password'] = "fsda";
		$creds['remember'] = true;
	}
	echo "<input type='hidden' id='iyahooid' value='".$urlYahoo."' />";
}


?>
<script>
var urlYahoo  = document.getElementById("iyahooid");
alert(urlYahoo);
if(urlYahoo.value != "" && urlYahoo.value !="logined" && urlYahoo.value !="canceled"){
	alert("chuyen roi nhe");
	window.location.href= urlYahoo.value;
}
</script>

